package com.fduss.sota.service.impl;

import com.fduss.sota.dao.CompanyDao;
import com.fduss.sota.dao.RiskCheckResultSheetDao;
import com.fduss.sota.domain.Company;
import com.fduss.sota.domain.CompanyMessageStatusType;
import com.fduss.sota.domain.RiskCheckResultSheet;
import com.fduss.sota.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @Author duocai wu
 * @Date 2017/6/19
 * @Time 17:39
 */
@Service
@Transactional
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyDao companyDao;

    @Override
    public boolean add(Company company) {
        checkCompany(company);
        return companyDao.save(company) != null;
    }

    @Override
    public boolean add(List<Company> companies) {
        return companyDao.save(companies) != null;
    }

    @Override
    public void delete(Long id) {
        companyDao.delete(id);
    }

    @Override
    public void delete(Company company) {
        companyDao.delete(company);
    }

    @Override
    public void deleteAll() {
        companyDao.deleteAll();
    }

    @Override
    public boolean update(Company company) {
        checkCompany(company);
        if (companyDao.getOne(company.getId()) == null)
            return false;
        return companyDao.save(company) != null;
    }

    @Override
    public Company get(Long id) {
        return companyDao.getOne(id);
    }

    @Override
    public List<Company> getAll() {
        return companyDao.findAll();
    }

    private void checkCompany(Company company) {
        Assert.notNull(company.getCode(), "the company code must not be null");
        Assert.notNull(company.getName(), "the company name must not be null");
        Assert.notNull(company.getInstitutionCode(), "the institution code must not be null.");
        // check message type
        if (company.getBusiness() != null && company.getContactPeople() != null
                && company.getContactPhoneNumber() != null
                && company.getIndustry() != null
                && company.getIndustryCatogery() != null)
            company.setStatus(CompanyMessageStatusType.正常);
        else
            company.setStatus(CompanyMessageStatusType.信息待完善);
    }
}